package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

//    1、请求方式  post
//    2、url地址  /user/login
//    3、请求参数  application/json     {“phone”:"13898765432"}
//    4、返回结果  无
    @PostMapping(value = "/login",name = "登录第一步，发送验证码")
    public ResponseEntity sendCode(@RequestBody Map map) throws Exception{
        String phone = (String) map.get("phone");
        return userService.sendCode(phone);
    }

//    1、请求方式  post
//    2、url地址  /user/loginVerification
//    3、请求参数  application/json     {“phone”:"13898765432","verificationCode":"123456"}
//    4、返回结果   {"token":"23234gerfweffger","isNew":true}
    @PostMapping(value = "/loginVerification",name = "登录第二步，校验验证码")
    public ResponseEntity loginVerification(@RequestBody Map map) throws Exception{
        String phone = (String) map.get("phone");
        String verificationCode = (String) map.get("verificationCode");
        return userService.loginVerification(phone,verificationCode);
    }

//    1、请求方式  post
//    2、url地址  /user/loginReginfo
//    3、请求参数  application/json   有请求头Authorization
//    {“phone”:"13898765432","verificationCode":"123456"}
//    4、返回结果   无
    @PostMapping(value = "/loginReginfo",name = "新用户填写资料")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token) throws Exception{
        return userService.loginReginfo(userInfo,token);
    }

//    1、请求方式  post
//    2、url地址  /user/loginReginfo/head
//    3、请求参数  multipart/form-data   有请求头Authorization
//    文件  文件参数名 headPhoto
//    4、返回结果   无
    @PostMapping(value = "/loginReginfo/head",name = "新用户上传头像")
    public ResponseEntity loginReginfoHead(MultipartFile headPhoto , @RequestHeader("Authorization") String token) throws Exception{
        return userService.loginReginfoHead(headPhoto,token);
    }



    @PostMapping("/save")
    public ResponseEntity save(User user){
        return userService.save(user);
    }

    @GetMapping("/findByMobile")
    public ResponseEntity findByMobile(String mobile){
        return userService.findByMobile(mobile);
    }


}
